'''
Created on 13.05.2019

@author: ahaemm
'''
import penaltymodel.core as pm
import networkx as nx
import dimod
from penaltymodel.cache.interface import cache_penalty_model
import numpy as np
from functions import createBeginTimeVariables

beginTimes_op1 = createBeginTimeVariables(1, 10) # the decision variables for the penalty model
#beginTimes_op2 = createBeginTimeVariables(2, 10) 
#beginTimes_op3 = createBeginTimeVariables(3, 10) 
#beginTimes_op4 = createBeginTimeVariables(4, 10)

feas_configs_sumBeginTimes = {(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 1, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 1, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 1, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 1, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 1, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 1, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 1, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 1, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 1)}

graph_sumBeginTimes_op1 = nx.Graph()
graph_sumBeginTimes_op1.add_edges_from([('x_1_1', 'x_1_2'), ('x_1_1', 'x_1_3'), ('x_1_1', 'x_1_4'), ('x_1_1', 'x_1_5'), ('x_1_1', 'x_1_6'), ('x_1_1', 'x_1_7'), ('x_1_1', 'x_1_8'), ('x_1_1', 'x_1_9'), ('x_1_1', 'x_1_10'),
                       ('x_1_2', 'x_1_3'), ('x_1_2', 'x_1_4'), ('x_1_2', 'x_1_5'), ('x_1_2', 'x_1_6'), ('x_1_2', 'x_1_7'), ('x_1_2', 'x_1_8'), ('x_1_2', 'x_1_9'), ('x_1_2', 'x_1_10'), 
                       ('x_1_3', 'x_1_4'), ('x_1_3', 'x_1_5'), ('x_1_3', 'x_1_6'), ('x_1_3', 'x_1_7'), ('x_1_3', 'x_1_8'), ('x_1_3', 'x_1_9'), ('x_1_3', 'x_1_10'),
                       ('x_1_4', 'x_1_5'), ('x_1_4', 'x_1_6'), ('x_1_4', 'x_1_7'), ('x_1_4', 'x_1_8'), ('x_1_4', 'x_1_9'), ('x_1_4', 'x_1_10'),
                       ('x_1_5', 'x_1_6'), ('x_1_5', 'x_1_7'), ('x_1_5', 'x_1_8'), ('x_1_5', 'x_1_9'), ('x_1_5', 'x_1_10'),
                       ('x_1_6', 'x_1_7'), ('x_1_6', 'x_1_8'), ('x_1_6', 'x_1_9'), ('x_1_6', 'x_1_10'), 
                       ('x_1_7', 'x_1_8'), ('x_1_7', 'x_1_9'), ('x_1_7', 'x_1_10'), 
                       ('x_1_8', 'x_1_9'), ('x_1_8', 'x_1_10'), 
                       ('x_1_9', 'x_1_10')])
spec_sumBeginTimes_op1 = pm.Specification(graph_sumBeginTimes_op1, beginTimes_op1, feas_configs_sumBeginTimes, dimod.BINARY)
qubo_sumBeginTimes_op1 = dimod.BinaryQuadraticModel({'x_1_1':-1., 'x_1_2':-1., 'x_1_3':-1., 'x_1_4':-1., 'x_1_5':-1., 'x_1_6':-1., 'x_1_7':-1., 'x_1_8':-1., 'x_1_9':-1., 'x_1_10':-1.,},
                               {('x_1_1', 'x_1_2'):2., ('x_1_1', 'x_1_3'):2., ('x_1_1', 'x_1_4'):2., ('x_1_1', 'x_1_5'):2., ('x_1_1', 'x_1_6'):2., ('x_1_1', 'x_1_7'):2., ('x_1_1', 'x_1_8'):2., ('x_1_1', 'x_1_9'):2., ('x_1_1', 'x_1_10'):2.,
                       ('x_1_2', 'x_1_3'):2., ('x_1_2', 'x_1_4'):2., ('x_1_2', 'x_1_5'):2., ('x_1_2', 'x_1_6'):2., ('x_1_2', 'x_1_7'):2., ('x_1_2', 'x_1_8'):2., ('x_1_2', 'x_1_9'):2., ('x_1_2', 'x_1_10'):2., 
                       ('x_1_3', 'x_1_4'):2., ('x_1_3', 'x_1_5'):2., ('x_1_3', 'x_1_6'):2., ('x_1_3', 'x_1_7'):2., ('x_1_3', 'x_1_8'):2., ('x_1_3', 'x_1_9'):2., ('x_1_3', 'x_1_10'):2.,
                       ('x_1_4', 'x_1_5'):2., ('x_1_4', 'x_1_6'):2., ('x_1_4', 'x_1_7'):2., ('x_1_4', 'x_1_8'):2., ('x_1_4', 'x_1_9'):2., ('x_1_4', 'x_1_10'):2.,
                       ('x_1_5', 'x_1_6'):2., ('x_1_5', 'x_1_7'):2., ('x_1_5', 'x_1_8'):2., ('x_1_5', 'x_1_9'):2., ('x_1_5', 'x_1_10'):2.,
                       ('x_1_6', 'x_1_7'):2., ('x_1_6', 'x_1_8'):2., ('x_1_6', 'x_1_9'):2., ('x_1_6', 'x_1_10'):2., 
                       ('x_1_7', 'x_1_8'):2., ('x_1_7', 'x_1_9'):2., ('x_1_7', 'x_1_10'):2., 
                       ('x_1_8', 'x_1_9'):2., ('x_1_8', 'x_1_10'):2., 
                       ('x_1_9', 'x_1_10'):2.}, 1.0,
                               dimod.BINARY)
"""
graph_sumBeginTimes_op2 = nx.Graph()
graph_sumBeginTimes_op2.add_edges_from([('x_2_1', 'x_2_2'), ('x_2_1', 'x_2_3'), ('x_2_1', 'x_2_4'), ('x_2_1', 'x_2_5'), ('x_2_1', 'x_2_6'), ('x_2_1', 'x_2_7'), ('x_2_1', 'x_2_8'), ('x_2_1', 'x_2_9'), ('x_2_1', 'x_2_10'),
                       ('x_2_2', 'x_2_3'), ('x_2_2', 'x_2_4'), ('x_2_2', 'x_2_5'), ('x_2_2', 'x_2_6'), ('x_2_2', 'x_2_7'), ('x_2_2', 'x_2_8'), ('x_2_2', 'x_2_9'), ('x_2_2', 'x_2_10'), 
                       ('x_2_3', 'x_2_4'), ('x_2_3', 'x_2_5'), ('x_2_3', 'x_2_6'), ('x_2_3', 'x_2_7'), ('x_2_3', 'x_2_8'), ('x_2_3', 'x_2_9'), ('x_2_3', 'x_2_10'),
                       ('x_2_4', 'x_2_5'), ('x_2_4', 'x_2_6'), ('x_2_4', 'x_2_7'), ('x_2_4', 'x_2_8'), ('x_2_4', 'x_2_9'), ('x_2_4', 'x_2_10'),
                       ('x_2_5', 'x_2_6'), ('x_2_5', 'x_2_7'), ('x_2_5', 'x_2_8'), ('x_2_5', 'x_2_9'), ('x_2_5', 'x_2_10'),
                       ('x_2_6', 'x_2_7'), ('x_2_6', 'x_2_8'), ('x_2_6', 'x_2_9'), ('x_2_6', 'x_2_10'), 
                       ('x_2_7', 'x_2_8'), ('x_2_7', 'x_2_9'), ('x_2_7', 'x_2_10'), 
                       ('x_2_8', 'x_2_9'), ('x_2_8', 'x_2_10'), 
                       ('x_2_9', 'x_2_10')])
spec_sumBeginTimes_op2 = pm.Specification(graph_sumBeginTimes_op2, beginTimes_op2, feas_configs_sumBeginTimes, dimod.BINARY)
qubo_sumBeginTimes_op2 = dimod.BinaryQuadraticModel({'x_2_1':-1., 'x_2_2':-1., 'x_2_3':-1., 'x_2_4':-1., 'x_2_5':-1., 'x_2_6':-1., 'x_2_7':-1., 'x_2_8':-1., 'x_2_9':-1., 'x_2_10':-1.,},
                               {('x_2_1', 'x_2_2'):2., ('x_2_1', 'x_2_3'):2., ('x_2_1', 'x_2_4'):2., ('x_2_1', 'x_2_5'):2., ('x_2_1', 'x_2_6'):2., ('x_2_1', 'x_2_7'):2., ('x_2_1', 'x_2_8'):2., ('x_2_1', 'x_2_9'):2., ('x_2_1', 'x_2_10'):2.,
                       ('x_2_2', 'x_2_3'):2., ('x_2_2', 'x_2_4'):2., ('x_2_2', 'x_2_5'):2., ('x_2_2', 'x_2_6'):2., ('x_2_2', 'x_2_7'):2., ('x_2_2', 'x_2_8'):2., ('x_2_2', 'x_2_9'):2., ('x_2_2', 'x_2_10'):2., 
                       ('x_2_3', 'x_2_4'):2., ('x_2_3', 'x_2_5'):2., ('x_2_3', 'x_2_6'):2., ('x_2_3', 'x_2_7'):2., ('x_2_3', 'x_2_8'):2., ('x_2_3', 'x_2_9'):2., ('x_2_3', 'x_2_10'):2.,
                       ('x_2_4', 'x_2_5'):2., ('x_2_4', 'x_2_6'):2., ('x_2_4', 'x_2_7'):2., ('x_2_4', 'x_2_8'):2., ('x_2_4', 'x_2_9'):2., ('x_2_4', 'x_2_10'):2.,
                       ('x_2_5', 'x_2_6'):2., ('x_2_5', 'x_2_7'):2., ('x_2_5', 'x_2_8'):2., ('x_2_5', 'x_2_9'):2., ('x_2_5', 'x_2_10'):2.,
                       ('x_2_6', 'x_2_7'):2., ('x_2_6', 'x_2_8'):2., ('x_2_6', 'x_2_9'):2., ('x_2_6', 'x_2_10'):2., 
                       ('x_2_7', 'x_2_8'):2., ('x_2_7', 'x_2_9'):2., ('x_2_7', 'x_2_10'):2., 
                       ('x_2_8', 'x_2_9'):2., ('x_2_8', 'x_2_10'):2., 
                       ('x_2_9', 'x_2_10'):2.}, 1.0,
                               dimod.BINARY)

graph_sumBeginTimes_op3 = nx.Graph()
graph_sumBeginTimes_op3.add_edges_from([('x_3_1', 'x_3_2'), ('x_3_1', 'x_3_3'), ('x_3_1', 'x_3_4'), ('x_3_1', 'x_3_5'), ('x_3_1', 'x_3_6'), ('x_3_1', 'x_3_7'), ('x_3_1', 'x_3_8'), ('x_3_1', 'x_3_9'), ('x_3_1', 'x_3_10'),
                       ('x_3_2', 'x_3_3'), ('x_3_2', 'x_3_4'), ('x_3_2', 'x_3_5'), ('x_3_2', 'x_3_6'), ('x_3_2', 'x_3_7'), ('x_3_2', 'x_3_8'), ('x_3_2', 'x_3_9'), ('x_3_2', 'x_3_10'), 
                       ('x_3_3', 'x_3_4'), ('x_3_3', 'x_3_5'), ('x_3_3', 'x_3_6'), ('x_3_3', 'x_3_7'), ('x_3_3', 'x_3_8'), ('x_3_3', 'x_3_9'), ('x_3_3', 'x_3_10'),
                       ('x_3_4', 'x_3_5'), ('x_3_4', 'x_3_6'), ('x_3_4', 'x_3_7'), ('x_3_4', 'x_3_8'), ('x_3_4', 'x_3_9'), ('x_3_4', 'x_3_10'),
                       ('x_3_5', 'x_3_6'), ('x_3_5', 'x_3_7'), ('x_3_5', 'x_3_8'), ('x_3_5', 'x_3_9'), ('x_3_5', 'x_3_10'),
                       ('x_3_6', 'x_3_7'), ('x_3_6', 'x_3_8'), ('x_3_6', 'x_3_9'), ('x_3_6', 'x_3_10'), 
                       ('x_3_7', 'x_3_8'), ('x_3_7', 'x_3_9'), ('x_3_7', 'x_3_10'), 
                       ('x_3_8', 'x_3_9'), ('x_3_8', 'x_3_10'), 
                       ('x_3_9', 'x_3_10')])
spec_sumBeginTimes_op3 = pm.Specification(graph_sumBeginTimes_op3, beginTimes_op3, feas_configs_sumBeginTimes, dimod.BINARY)
qubo_sumBeginTimes_op3 = dimod.BinaryQuadraticModel({'x_3_1':-1., 'x_3_2':-1., 'x_3_3':-1., 'x_3_4':-1., 'x_3_5':-1., 'x_3_6':-1., 'x_3_7':-1., 'x_3_8':-1., 'x_3_9':-1., 'x_3_10':-1.,},
                               {('x_3_1', 'x_3_2'):2., ('x_3_1', 'x_3_3'):2., ('x_3_1', 'x_3_4'):2., ('x_3_1', 'x_3_5'):2., ('x_3_1', 'x_3_6'):2., ('x_3_1', 'x_3_7'):2., ('x_3_1', 'x_3_8'):2., ('x_3_1', 'x_3_9'):2., ('x_3_1', 'x_3_10'):2.,
                       ('x_3_2', 'x_3_3'):2., ('x_3_2', 'x_3_4'):2., ('x_3_2', 'x_3_5'):2., ('x_3_2', 'x_3_6'):2., ('x_3_2', 'x_3_7'):2., ('x_3_2', 'x_3_8'):2., ('x_3_2', 'x_3_9'):2., ('x_3_2', 'x_3_10'):2., 
                       ('x_3_3', 'x_3_4'):2., ('x_3_3', 'x_3_5'):2., ('x_3_3', 'x_3_6'):2., ('x_3_3', 'x_3_7'):2., ('x_3_3', 'x_3_8'):2., ('x_3_3', 'x_3_9'):2., ('x_3_3', 'x_3_10'):2.,
                       ('x_3_4', 'x_3_5'):2., ('x_3_4', 'x_3_6'):2., ('x_3_4', 'x_3_7'):2., ('x_3_4', 'x_3_8'):2., ('x_3_4', 'x_3_9'):2., ('x_3_4', 'x_3_10'):2.,
                       ('x_3_5', 'x_3_6'):2., ('x_3_5', 'x_3_7'):2., ('x_3_5', 'x_3_8'):2., ('x_3_5', 'x_3_9'):2., ('x_3_5', 'x_3_10'):2.,
                       ('x_3_6', 'x_3_7'):2., ('x_3_6', 'x_3_8'):2., ('x_3_6', 'x_3_9'):2., ('x_3_6', 'x_3_10'):2., 
                       ('x_3_7', 'x_3_8'):2., ('x_3_7', 'x_3_9'):2., ('x_3_7', 'x_3_10'):2., 
                       ('x_3_8', 'x_3_9'):2., ('x_3_8', 'x_3_10'):2., 
                       ('x_3_9', 'x_3_10'):2.}, 1.0,
                               dimod.BINARY)

graph_sumBeginTimes_op4 = nx.Graph()
graph_sumBeginTimes_op4.add_edges_from([('x_4_1', 'x_4_2'), ('x_4_1', 'x_4_3'), ('x_4_1', 'x_4_4'), ('x_4_1', 'x_4_5'), ('x_4_1', 'x_4_6'), ('x_4_1', 'x_4_7'), ('x_4_1', 'x_4_8'), ('x_4_1', 'x_4_9'), ('x_4_1', 'x_4_10'),
                       ('x_4_2', 'x_4_3'), ('x_4_2', 'x_4_4'), ('x_4_2', 'x_4_5'), ('x_4_2', 'x_4_6'), ('x_4_2', 'x_4_7'), ('x_4_2', 'x_4_8'), ('x_4_2', 'x_4_9'), ('x_4_2', 'x_4_10'), 
                       ('x_4_3', 'x_4_4'), ('x_4_3', 'x_4_5'), ('x_4_3', 'x_4_6'), ('x_4_3', 'x_4_7'), ('x_4_3', 'x_4_8'), ('x_4_3', 'x_4_9'), ('x_4_3', 'x_4_10'),
                       ('x_4_4', 'x_4_5'), ('x_4_4', 'x_4_6'), ('x_4_4', 'x_4_7'), ('x_4_4', 'x_4_8'), ('x_4_4', 'x_4_9'), ('x_4_4', 'x_4_10'),
                       ('x_4_5', 'x_4_6'), ('x_4_5', 'x_4_7'), ('x_4_5', 'x_4_8'), ('x_4_5', 'x_4_9'), ('x_4_5', 'x_4_10'),
                       ('x_4_6', 'x_4_7'), ('x_4_6', 'x_4_8'), ('x_4_6', 'x_4_9'), ('x_4_6', 'x_4_10'), 
                       ('x_4_7', 'x_4_8'), ('x_4_7', 'x_4_9'), ('x_4_7', 'x_4_10'), 
                       ('x_4_8', 'x_4_9'), ('x_4_8', 'x_4_10'), 
                       ('x_4_9', 'x_4_10')])
spec_sumBeginTimes_op4 = pm.Specification(graph_sumBeginTimes_op4, beginTimes_op4, feas_configs_sumBeginTimes, dimod.BINARY)
#penaltyModel_sumBeginTimes_op1 = pm.get_penalty_model(spec_sumBeginTimes_op1)
qubo_sumBeginTimes_op4 = dimod.BinaryQuadraticModel({'x_4_1':-1., 'x_4_2':-1., 'x_4_3':-1., 'x_4_4':-1., 'x_4_5':-1., 'x_4_6':-1., 'x_4_7':-1., 'x_4_8':-1., 'x_4_9':-1., 'x_4_10':-1.,},
                               {('x_4_1', 'x_4_2'):2., ('x_4_1', 'x_4_3'):2., ('x_4_1', 'x_4_4'):2., ('x_4_1', 'x_4_5'):2., ('x_4_1', 'x_4_6'):2., ('x_4_1', 'x_4_7'):2., ('x_4_1', 'x_4_8'):2., ('x_4_1', 'x_4_9'):2., ('x_4_1', 'x_4_10'):2.,
                       ('x_4_2', 'x_4_3'):2., ('x_4_2', 'x_4_4'):2., ('x_4_2', 'x_4_5'):2., ('x_4_2', 'x_4_6'):2., ('x_4_2', 'x_4_7'):2., ('x_4_2', 'x_4_8'):2., ('x_4_2', 'x_4_9'):2., ('x_4_2', 'x_4_10'):2., 
                       ('x_4_3', 'x_4_4'):2., ('x_4_3', 'x_4_5'):2., ('x_4_3', 'x_4_6'):2., ('x_4_3', 'x_4_7'):2., ('x_4_3', 'x_4_8'):2., ('x_4_3', 'x_4_9'):2., ('x_4_3', 'x_4_10'):2.,
                       ('x_4_4', 'x_4_5'):2., ('x_4_4', 'x_4_6'):2., ('x_4_4', 'x_4_7'):2., ('x_4_4', 'x_4_8'):2., ('x_4_4', 'x_4_9'):2., ('x_4_4', 'x_4_10'):2.,
                       ('x_4_5', 'x_4_6'):2., ('x_4_5', 'x_4_7'):2., ('x_4_5', 'x_4_8'):2., ('x_4_5', 'x_4_9'):2., ('x_4_5', 'x_4_10'):2.,
                       ('x_4_6', 'x_4_7'):2., ('x_4_6', 'x_4_8'):2., ('x_4_6', 'x_4_9'):2., ('x_4_6', 'x_4_10'):2., 
                       ('x_4_7', 'x_4_8'):2., ('x_4_7', 'x_4_9'):2., ('x_4_7', 'x_4_10'):2., 
                       ('x_4_8', 'x_4_9'):2., ('x_4_8', 'x_4_10'):2., 
                       ('x_4_9', 'x_4_10'):2.}, 1.0,
                               dimod.BINARY)
"""
pModel_sumBeginTimes_op1 = pm.PenaltyModel.from_specification(spec_sumBeginTimes_op1, qubo_sumBeginTimes_op1, 2., 0.)
cache_penalty_model(pModel_sumBeginTimes_op1, 'c:\\Users\\ahaemm\\workspace\\dwave\\env\\data\\dwave-penaltymodel-cache\\penaltymodel_cache_v0.4.0.db')

#pModel_sumBeginTimes_op2 = pm.PenaltyModel.from_specification(spec_sumBeginTimes_op2, qubo_sumBeginTimes_op2, 2, 0)
#cache_penalty_model(pModel_sumBeginTimes_op2, 'c:\\Users\\ahaemm\\workspace\\dwave\\env\\data\\dwave-penaltymodel-cache\\penaltymodel_cache_v0.4.0.db')

#pModel_sumBeginTimes_op3 = pm.PenaltyModel.from_specification(spec_sumBeginTimes_op3, qubo_sumBeginTimes_op3, 2, 0)
#cache_penalty_model(pModel_sumBeginTimes_op3, 'c:\\Users\\ahaemm\\workspace\\dwave\\env\\data\\dwave-penaltymodel-cache\\penaltymodel_cache_v0.4.0.db')

#pModel_sumBeginTimes_op4 = pm.PenaltyModel.from_specification(spec_sumBeginTimes_op4, qubo_sumBeginTimes_op4, 2, 0)
#cache_penalty_model(pModel_sumBeginTimes_op4, 'c:\\Users\\ahaemm\\workspace\\dwave\\env\\data\\dwave-penaltymodel-cache\\penaltymodel_cache_v0.4.0.db')

#pm_tmp = get_penalty_model(spec_sumBeginTimes_op4, 'c:\\Users\\ahaemm\\workspace\\dwave\\env\\data\\dwave-penaltymodel-cache\\penaltymodel_cache_v0.4.0.db')
